//
//  HTGoodsSpecialVC.m
//  TruckUser
//
//  Created by Ting Han on 2017/10/16.
//  Copyright © 2017年 Ting Han. All rights reserved.
//

#import "HTGoodsSpecialVC.h"
#import "UIView+Layout.h"
#import "HTGoodsSpecialCell.h"
#import "HTGoodsListVC.h"
#import "HTVoiceRemindCell.h"
#import "HTAddSpecialVC.h"
@interface HTGoodsSpecialVC ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *dataSource;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *safeAreaBottom;
@property (weak, nonatomic) IBOutlet UIButton *conformButton;


@end

@implementation HTGoodsSpecialVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.safeAreaBottom.constant = kHTToolBarHeight + IPHONE_SAFEBOTTOMAREA_HEIGHT + IPHONE_NAVIGATIONBAR_HEIGHT;
    [self configTableView];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.tableView.mj_header beginRefreshing];
    
}
- (void)configTableView{
    self.conformButton.layer.masksToBounds = YES;
    self.conformButton.layer.cornerRadius = kWidth(4);
    self.conformButton.layer.borderColor = kBlueColor.CGColor;
    self.conformButton.layer.borderWidth = 1;
    [self.conformButton addTarget:self action:@selector(didConformAddNew) forControlEvents:UIControlEventTouchUpInside];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.width = kHTScreenWidth;
    self.tableView.backgroundColor = kBackgroundColor;
     [self.tableView registerNib:[UINib nibWithNibName:@"HTVoiceRemindCell" bundle:nil] forCellReuseIdentifier:voiceCell];
    [self.tableView registerNib:[UINib nibWithNibName:@"HTGoodsSpecialCell" bundle:nil] forCellReuseIdentifier:GoodsSpecialCell];
    [self configTableHeader];
    self.tableView.mj_header = self.header;
    self.noDataView = [[HTNoDataView alloc]initWithFrame:self.tableView.bounds];
    self.noDataView.showMessage = @"暂无订阅路线，请添加";
    
}

- (NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
    
}
#pragma mark --- tableView
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    return @"删除";
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return UITableViewCellEditingStyleNone;
    }
    return UITableViewCellEditingStyleDelete;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return;
    }
    if (indexPath.row == 0) {
        return;
    }
    [UIAlertController alertStyleShowInController:self title:@"温馨提示" message:@"是否删除此线路" alertActionsBlock:^(NSMutableArray<UIAlertAction *> *alertActions) {
        [alertActions addObject:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }]];
        [alertActions addObject:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [self netDeleteDataModel:self.dataSource[indexPath.row - 1]indexPath:indexPath];
            
        }]];
    } completion:^{
        
    }];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
   
    NSInteger count = 0;
    if (self.dataSource.count >= 1) {
        count = self.dataSource.count + 1;
    }else{
        count = 0;
    }
    if (section == 0) {
        if (count == 0) {
            return 0;
        }
        return 1;
    }
    [tableView tableViewDisplayView:self.noDataView ifNecessaryForRowCount:count];
    return count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        HTVoiceRemindCell *cell = [tableView dequeueReusableCellWithIdentifier:voiceCell forIndexPath:indexPath];
        cell.voiceSwitch.on = [HTMUserDefaultUtil getPlayMusicFlag];
        [cell.voiceSwitch addTarget:self action:@selector(swichAction:) forControlEvents:UIControlEventValueChanged];
        return cell;
    }
    HTGoodsSpecialCell *cell = [tableView dequeueReusableCellWithIdentifier:GoodsSpecialCell forIndexPath:indexPath];
    if (indexPath.row == 0) {
        [cell setupAllLine];
    }else{
        cell.goodsSpecialModel = self.dataSource[indexPath.row - 1];
    }
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {//voice
        return 59.f;
    }else if (indexPath.row == self.dataSource.count + 1) {
        return kWidth(60);
    }
    return 50;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]initWithFrame:tableView.tableHeaderView.bounds];
    view.backgroundColor = kBackgroundColor;
    return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 1) {
        return 20.f;
    }
    return 0.f;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HTGoodsListVC *goods = [[HTGoodsListVC alloc]init];
    if (indexPath.row == 0) {
         goods.title = @"全部线路";
        goods.ID = @"全部";
    }else{
        HTGoodsSpecialModel *model = self.dataSource[indexPath.row - 1];
        goods.title = [NSString stringWithFormat:@"%@-%@",model.from,model.to];
        goods.ID = model.ID;
    }
    [self.navigationController pushViewController:goods animated:YES];

}
- (void)swichAction:(UISwitch *)sender{
    BOOL isButtonOn = [sender isOn];
    [HTMUserDefaultUtil savePlayMusicFlag:isButtonOn];
    HTVoiceRemindCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    if (isButtonOn) {
        //开
        cell.messageStr = @"货源声音提醒(已开启)";
    }else {
        //关
        cell.messageStr = @"货源声音提醒(已关闭)";
    }
}
- (void)didConformAddNew{
    HTAddSpecialVC *addSpecial = [[HTAddSpecialVC alloc]init];
    [self.navigationController pushViewController:addSpecial animated:YES];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView == self.tableView)
    {
        CGFloat sectionHeaderHeight = 20; //sectionHeaderHeight
        if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
            scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
        } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
            scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
        }
    }
}
#pragma mark --- 网络请求
- (void)netDeleteDataModel:(HTGoodsSpecialModel *)model indexPath:(NSIndexPath *)indexPath{
    [self startHud];
    [HTHttpRequestManager requestOrderlineDeleteWithId:model.ID success:^(id  _Nullable responseObject) {
        [self endHud];
        if (!responseObject) {
            [HTShowUtil showMessage:@"删除线路失败"];
            return;
        }
        NSString *codeStr = [NSString stringWithFormat:@"%@",responseObject[@"code"]];
        if (![codeStr isEqualToString:@"200"]) {
            [HTShowUtil showMessage:@"删除线路失败"];
            return;
        }
        [HTShowUtil showMessage:@"删除线路成功"];
        [self.tableView.mj_header beginRefreshing];
    } failure:^(NSError * _Nonnull error) {
        [self endHud];
        [HTShowUtil showMessage:@"网络错误"];
    }];
}
- (void)loadNewData{
    [self netLoadData];
}
- (void)netLoadData{
    [HTHttpRequestManager requestOrderlineListSuccess:^(id  _Nullable responseObject) {
        [self.tableView.mj_header endRefreshing];
        DBLog(@"订阅列表：%@",responseObject);
        if (!responseObject) {
            [HTShowUtil showMessage:@"订阅列表请求失败"];
            return;
        }
        NSString *codeStr = [NSString stringWithFormat:@"%@",responseObject[@"code"]];
        if (![codeStr isEqualToString:@"200"]) {
            [HTShowUtil showMessage:responseObject[@"message"]];
            return;
        }
        self.dataSource = [HTGoodsSpecialModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"]];
        [self.tableView reloadData];
    } failure:^(NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
    }];
}
@end
